AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

function ENT:Initialize(model)
	self:SetNWEntity(ID_PREV_ENTITY, null)
	self:SetNWEntity(ID_NEXT_ENTITY, null)
	self:SetNWInt(ID_NODE_ID, -1)
	local defaultPathDir = Vector(0,0,200)
	self:SetNWVector(ID_PATH_DIRECTION, defaultPathDir)

	if model then
		self:SetModel(model)
	else
		self:SetModel(Model("models/Combine_Helicopter/helicopter_bomb01.mdl"))
	end

	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:DrawShadow(false)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_WORLD)

	local phys = self.Entity:GetPhysicsObject()
	if phys:IsValid() then
		phys:Sleep()
	end

end

//****************************************************
//* Invokes functionality to remove a camera from its
//* associated track
function ENT:Delete()

	local prevNode = self.BaseClass.GetPrevNode(self)
	local nextNode = self.BaseClass.GetNextNode(self)

	//We are not the root node
	if self.BaseClass.IsValid(self, prevNode) then

		//We are not the end node
		if self.BaseClass.IsValid(self, nextNode) then
			prevNode:SetNWEntity(ID_NEXT_ENTITY, nextNode)
			nextNode:SetNWEntity(ID_PREV_ENTITY, prevNode)
		else
			prevNode:SetNWEntity(ID_NEXT_ENTITY, nil)
		end

	//We are the root node
	elseif self.BaseClass.IsValid(self, nextNode) then

		//TODO: ensure that all "TRACK WIDE" variables are passed on!!!
		nextNode:SetNWString(ID_ENTITY_NAME, self:GetNWString(ID_ENTITY_NAME))
		nextNode:SetNWEntity(ID_PREV_ENTITY, nil)
			
	end
	
	//Reconstruct numbering
	local rootNode = self.BaseClass.GetRootNode(self)
	if rootNode == self then
		rootNode = self.BaseClass.GetNextNode(self)
	end

	local currentID = 1
	while self.BaseClass.IsValid(self, rootNode) do
		rootNode:SetNWInt(ID_NODE_ID,currentID)	
		rootNode = rootNode.BaseClass.GetNextNode(rootNode)
		currentID = currentID + 1
	end

	if self.OnDelete then
		self:OnDelete()
	end
	self:Remove()

end

//****************************************************
//* Invokes functionality to remove an entrie camera
//* track
function ENT:DeleteTrack()

	local rootNode = self.BaseClass.GetRootNode(self)
	
	if self == rootNode then

		local onNode = self.BaseClass.GetNextNode(self)
		local pathDirA = nil
		local pathDirB = nil
	
		while self.BaseClass.IsValid(self,onNode) do
			local onNextNode = onNode.BaseClass.GetNextNode(onNode)

			pathDirA = onNode:GetNWEntity(ID_PATH_DIRECTION_ENTITY_A)
			if self.BaseClass.IsValid(self,pathDirA) then pathDirA:Remove()	end
			pathDirB = onNode:GetNWEntity(ID_PATH_DIRECTION_ENTITY_B)
			if self.BaseClass.IsValid(self,pathDirB) then pathDirB:Remove()	end
			
			if onNode.OnDelete then
				onNode:OnDelete()
			end

			onNode:Remove()
			onNode = onNextNode
		end

		if self.OnDelete then
			self:OnDelete()
		end
		self:Remove()

	else
		rootNode.BaseClass.DeleteTrack(rootNode)
	end
end
